package utils;

import book.Book;
import com.bit.utils.FileUtils;
import constant.Constant;

import java.io.IOException;
import java.time.LocalDate;
import java.time.LocalDateTime;

public class AnalyzingBook {

    public void storeObject(Book[] books,String filename) throws IOException {

        //有效的书籍
        int bookUseLen = 0;

        for (int i = 0; i < books.length; i++) {
            if(books[i] != null){
                bookUseLen++;
            }
        }

        StringBuilder jsonArray = new StringBuilder();
        for (int i = 0; i < bookUseLen; i++) {

            //把每本书的信息 分割成字符串 用,隔开
            if(books[i] != null){
                jsonArray.append(books[i].toJson());

                //每本书的最后一个信息 加上\n 因为当前把所有的书的信息都串在了一起
                if(i != bookUseLen - 1){
                    jsonArray.append("\n");
                }
            }
        }

        //把内存当中 存储的数据 写到磁盘上
        FileUtils.writeFile(String.valueOf(jsonArray),filename);

    }

    public Book[] loadObject(String filename) throws IOException {

        //1.从磁盘文件中读取数据
        String content = FileUtils.readFile(filename);

        if (content == null || content.equals("")) {
            System.out.println("图书馆为空！");
            return null;
        }

        //2.使用\n把所有书籍的字符串进行分割
        String[] bookJsonStrings = content.split("\n");

        //3.把分割后的字符串再进行分割,取到ISBN、title、author...
        Book[] bookList = new Book[bookJsonStrings.length];
        for (int i = 0; i < bookJsonStrings.length; i++) {
            Book book = parseBookJson(bookJsonStrings[i]);
            bookList[i] = book;
        }

        return bookList;
    }

    //进行分割 取到ISBN,title,author...
    private Book parseBookJson(String bookJsonString) {
        String[] pairs = bookJsonString.split(",");

        int ISBN = Integer.parseInt(pairs[0]);
        String title = pairs[1];
        String author = pairs[2];
        int publishYear = Integer.parseInt(pairs[3]);
        boolean isBorrowed = Boolean.parseBoolean(pairs[4]);
        int borrowedCount = Integer.parseInt(pairs[5]);
        LocalDateTime shelfDate = LocalDateTime.parse(pairs[6]);

        if(title != null && author != null && shelfDate != null) {
            Book book = new Book(title,author,publishYear,shelfDate);
            book.setISBN(ISBN);
            book.setBorrowed(isBorrowed);
            book.setBorrowedCount(borrowedCount);
            return book;
        }

        return null;

    }


    public static void main(String[] args) {
        Book[] books = new Book[4];
        books[0] = new Book("西游记","吴承恩",2007, LocalDateTime.of(2003,4,23,0,0));
        books[1] = new Book("水浒传","施耐庵",2013, LocalDateTime.of(2006,6,5,0,0));
        books[2] = new Book("三国演义","罗贯中",2004, LocalDateTime.of(2010,9,14,0,0));
        books[3] = new Book("红楼梦","曹雪芹",2010, LocalDateTime.of(2011,3,1,0,0));

        AnalyzingBook analyzingBook = new AnalyzingBook();

        
        try {
            analyzingBook.storeObject(books, Constant.ALL_BOOKS_FILE_NAME);

            Book[] ret = analyzingBook.loadObject(Constant.ALL_BOOKS_FILE_NAME);

        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }


}
